package org.hxyjs.service.impl;

import org.hxyjs.dao.TicketRecordDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.TicketRecord;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.TicketRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("TicketRecordService")
public class TicketRecordServiceImpl implements TicketRecordService {
    @Resource
    TicketRecordDao ticketRecordDao;
    @Override
    public DTO<TicketRecord> getAllTicketRecords()  throws Exception{
        List<TicketRecord> ticketRecords = ticketRecordDao.getAllTicketRecords();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(ticketRecords).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<TicketRecord>(messageEnum.getCode(),messageEnum.getMessage(),ticketRecords);
    }

    @Override
    public DTO<TicketRecord> deleteTicketRecordByArcno(String arcno)  throws Exception{
        Integer result = ticketRecordDao.deleteTicketRecordByArcno(arcno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<TicketRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<TicketRecord> getTicketRecordByArcno(String arcno)  throws Exception{
        TicketRecord ticketRecords = ticketRecordDao.getTicketRecordByArcno(arcno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(ticketRecords).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<TicketRecord>(messageEnum.getCode(),messageEnum.getMessage(),ticketRecords);
    }

    @Override
    public DTO<TicketRecord> updateTicketRecordByArcno(TicketRecord ticketRecord) throws Exception {
        Integer result = ticketRecordDao.updateTicketRecordByArcno(ticketRecord);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<TicketRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<TicketRecord> addTicketRecord(TicketRecord ticketRecord) throws Exception {
        Integer result = ticketRecordDao.addTicketRecord(ticketRecord);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<TicketRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public DTO<TicketRecord> getTicketRecordByPage(PageInfo pageInfo) throws Exception {
        List<TicketRecord> ticketRecords = ticketRecordDao.getTicketRecordByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(ticketRecords).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<TicketRecord>(messageEnum.getCode(),messageEnum.getMessage(),ticketRecords,pageInfo);
    }

}
